package com.pritive.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pritive.bean.User;
import com.pritive.service.UserService;
import com.pritive.service.serviceImpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
     UserService userService =  new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String username = req.getParameter("username");

        List<User> users = userService.selectLike(username);

        ObjectMapper mapper = new ObjectMapper();

        String valueAsString = mapper.writeValueAsString(users);

        resp.getWriter().write(valueAsString);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
